package com.qd.p2p.web.action.additional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.UpRateService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.RateInterest;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.RateInterestModel;

/**
 * 加息券
 * @author ylx
 * @version 2.0
 * @date 2015年5月19日 上午11:11:27
 */
public class UserUprateAction extends BaseAction<RateInterestModel> implements ModelDriven<RateInterestModel> {

	@Resource
	private UpRateService upRateService;
	
	private Map<String, Object> data ;

	
	/**
	 * 
	 * 我的加息券列表 使用status2 json包返回数据，懒加截会序列化到前台,如果使用getStringOfJpaObj 则不会
	 * 
	 * @return
	 */
	@Action(value = "/uprate/myRateInterestList", interceptorRefs = {@InterceptorRef("session"), @InterceptorRef("globalStack") })
	public void myRateInterestList() throws Exception{
		User user = getSessionUser();
		data = new HashMap<String, Object>();
		int pageNumber = paramInt("page"); // 当前页数
		int pageSize = paramInt("rows"); // 每页每页总数
		if (pageSize<=0){
			pageSize=5;
		}
		PageDataList<RateInterest> rateList =upRateService.getRateInterestList(user.getUserId(), model.getStatus(), pageNumber, pageSize);
		data.put("total", rateList.getPage().getTotal()); // 总行数
		data.put("data", rateList); // 集合对象
		data.put("status", model.getStatus());
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 
	 * 获取我的加息券可用列表
	 * 
	 * @return
	 */
	@Action(value="/uprate/useable", interceptorRefs = {@InterceptorRef("session"), @InterceptorRef("globalStack") })
	public void useable() throws Exception {
		User user = getSessionUser();
		
		List<RateInterest> list = upRateService.getRateInterestList(user.getUserId());
		data = new HashMap<String, Object>();
		data.put("rows", list); // 集合对象
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 
	 * 获取我的加息券可用列表
	 * 
	 * @return
	 */
	@Action(value="/uprate/initUseable", interceptorRefs = {@InterceptorRef("session"), @InterceptorRef("globalStack") })
	public void initUseable() throws Exception {
		User user = getSessionUser();
		int count = upRateService.countRateInterest(user.getUserId());
		data = new HashMap<String, Object>();
		data.put("count", count); // 集合对象
		printJson(getStringOfJpaObj(data));
	}
}
